using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharaterAIController : Character
{



    public int charaterStateChangeTime = 100;
    public string pursueEnemyTag;
    public Vector2 pursueRange;
    public CharaterAIEeum state;

    public float attackDesire = 0.5f;

    private Transform enemyTransform;
    private int currentTime;


    public override void Start()
    {
        base.Start();
        enemyTransform = GameObject.FindGameObjectWithTag(pursueEnemyTag).transform;
        currentTime = charaterStateChangeTime;
        //state = CharaterAIEeum.IDLE;
    }

    public override void Update()
    {
        base.Update();
        ClipCanMove(new string[] { "idle", "walk", "run", "jump", "jump_fall" });
        StateAI();
    }

    public override void FixedUpdate()
    {
        base.FixedUpdate();
    }



    private void StateAI()
    {
        if (--currentTime <= 0)
        {
            currentTime = Random.Range(charaterStateChangeTime, charaterStateChangeTime + 50);
            float instanceX = enemyTransform.position.x - transform.position.x;
            float instanceY = enemyTransform.position.y - transform.position.y;
            if (Mathf.Abs(instanceX) > pursueRange.x || Mathf.Abs(instanceY) > pursueRange.y)
            {
                if(Random.value <= attackDesire)
                    PursueAI(new Vector2(instanceX, instanceY));
                else
                    IdleAI();
            }
            else
            {
                float random = Random.value;
                if (random <= attackDesire)
                {
                    if (getCurrentClipInfo().Equals("idle"))
                    {
                        SetScaleX((int)Mathf.Sign(instanceX));
                    }
                    state = CharaterAIEeum.ATTACK;
                    childAnimator.SetTrigger("CommonAttack");
                }
                else if (random <= attackDesire + 0.2f)
                {
                    MoveAI();
                }
                else
                {
                    IdleAI();
                }
            }
        }




    }

    private void MoveAI()
    {
        state = CharaterAIEeum.WLAK;
        while (inputMove == Vector2.zero) 
        { 
            inputMove.x = Random.Range(-1, 2);
            inputMove.y = Random.Range(-1, 2);
        }
        isRun = Random.value < attackDesire;
    }

    private void IdleAI()
    {
        state = CharaterAIEeum.IDLE;
        inputMove = Vector2.zero;
        isRun = false;
        isWalk = false;
    }

    private void PursueAI(Vector2 instance)
    {
        if (Mathf.Abs(instance.x) > pursueRange.x)
        {
            state = CharaterAIEeum.WLAK;
            inputMove.x = Mathf.Sign(instance.x);
            isRun = true;
        }
        if (Mathf.Abs(instance.y) > pursueRange.y)
        {
            state = CharaterAIEeum.WLAK;
            inputMove.y = Mathf.Sign(instance.y);
        }
        state = CharaterAIEeum.PURSUE;
    }
}
